package com.i2works.smartluolong.api.controller.fooddrug;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.flow.FlowInfoDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyDeviceDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyDeviceQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyImagesDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyImagesQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyLicenseDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyLicenseQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyMemberDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyMemberQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeDeviceDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeDeviceQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeImagesDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeImagesQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeLicenseDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeLicenseQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeMemberDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeMemberQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugChangeQuery;
import com.i2works.smartluolong.web.service.flow.IFlowInfoService;
import com.i2works.smartluolong.web.service.flow.IFlowProcessService;
import com.i2works.smartluolong.web.service.flow.IFlowService;
import com.i2works.smartluolong.web.service.flow.IFlowTemplateService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugApplyDeviceService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugApplyImagesService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugApplyLicenseService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugApplyMemberService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugApplyService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugChangeDeviceService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugChangeImagesService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugChangeLicenseService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugChangeMemberService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugChangeService;
import com.i2works.smartluolong.api.controller.common.CommonItemController;
import com.i2works.smartluolong.utils.base.BusinessCategoryEnum;
import com.i2works.smartluolong.utils.base.BusinessProjectUtil;
import com.i2works.smartluolong.utils.base.FooddrugConstant;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.base.NationUtil;
/**
 * 食药申请控制器
 *
 * @author Liu YaPeng
 * @date 2017/9/27 20:07
 */

@Controller
@RequestMapping("/fooddrugChange")
public class FooddrugChangeController extends CommonItemController {


	private final IFooddrugApplyService fooddrugApplyService;

	private final IFooddrugApplyImagesService fooddrugApplyImagesService;

	private final IFooddrugApplyMemberService fooddrugApplyMemberService;

	private final IFooddrugApplyDeviceService fooddrugApplyDeviceService;

	private final IFooddrugApplyLicenseService fooddrugApplyLicenseService;


	private final IFooddrugChangeService fooddrugChangeService;

	private final IFooddrugChangeImagesService fooddrugChangeImagesService;

	private final IFooddrugChangeMemberService fooddrugChangeMemberService;

	private final IFooddrugChangeDeviceService fooddrugChangeDeviceService;

	private final IFooddrugChangeLicenseService fooddrugChangeLicenseService;
	
    private final IFlowInfoService flowInfoService;
    

	@Autowired
	public FooddrugChangeController(
			IFooddrugApplyService fooddrugApplyService,IFooddrugApplyImagesService fooddrugApplyImagesService,IFooddrugApplyMemberService fooddrugApplyMemberService,IFooddrugApplyDeviceService fooddrugApplyDeviceService,IFooddrugApplyLicenseService fooddrugApplyLicenseService,
			IFooddrugChangeService fooddrugChangeService,IFooddrugChangeImagesService fooddrugChangeImagesService,IFooddrugChangeMemberService fooddrugChangeMemberService,IFooddrugChangeDeviceService fooddrugChangeDeviceService,IFooddrugChangeLicenseService fooddrugChangeLicenseService,
			IFlowInfoService flowInfoService,IFlowTemplateService flowTemplateService, IFlowService flowService, IFlowProcessService flowProcessService
			) {
		super(flowTemplateService, flowService, flowProcessService);
		this.fooddrugApplyService=fooddrugApplyService;
		this.fooddrugApplyImagesService=fooddrugApplyImagesService;
		this.fooddrugApplyMemberService=fooddrugApplyMemberService;
		this.fooddrugApplyDeviceService=fooddrugApplyDeviceService;
		this.fooddrugApplyLicenseService=fooddrugApplyLicenseService;
		this.fooddrugChangeService=fooddrugChangeService;
		this.fooddrugChangeImagesService=fooddrugChangeImagesService;
		this.fooddrugChangeMemberService=fooddrugChangeMemberService;
		this.fooddrugChangeDeviceService=fooddrugChangeDeviceService;
		this.fooddrugChangeLicenseService=fooddrugChangeLicenseService;
		this.flowInfoService=flowInfoService;
	}


	private FooddrugChangeDO applyToChange(Integer applyId) {
		FooddrugChangeDO fooddrugChangeDO=new FooddrugChangeDO();
		try {
			FooddrugApplyQuery query=new FooddrugApplyQuery();
			query.setId(applyId);
			FooddrugApplyDO fooddrugApplyDO =fooddrugApplyService.query(query);
			fooddrugChangeDO.setApplyId(applyId);
			fooddrugChangeDO.setCompanyName(fooddrugApplyDO.getCompanyName());
			fooddrugChangeDO.setUserId(getUserId());
			fooddrugChangeDO.setApplyStatus(0);
			fooddrugChangeDO.setCreateName(getUserName());
			fooddrugChangeDO.setUpdateName(getUserName());
			fooddrugChangeService.save(fooddrugChangeDO);
			FooddrugApplyImagesQuery imgQuery=new FooddrugApplyImagesQuery();
			imgQuery.setApplyId(applyId);
			List<FooddrugApplyImagesDO>   list=fooddrugApplyImagesService.queryList(imgQuery);
			for (FooddrugApplyImagesDO fooddrugApplyImages : list) {
				FooddrugChangeImagesDO  fooddrugChangeImages=new FooddrugChangeImagesDO();
				fooddrugChangeImages.setChangeId(fooddrugChangeDO.getId());
				fooddrugChangeImages.setImg(fooddrugApplyImages.getImg());
				fooddrugChangeImages.setType(fooddrugApplyImages.getType());
				fooddrugChangeImagesService.save(fooddrugChangeImages);
			}
			FooddrugApplyMemberQuery memberQuery=new FooddrugApplyMemberQuery();
			memberQuery.setApplyId(applyId);
			List<FooddrugApplyMemberDO> memberList=fooddrugApplyMemberService.queryList(memberQuery);
			for (FooddrugApplyMemberDO fooddrugApplyMemberDO : memberList) {
				FooddrugChangeMemberDO fooddrugChangeMemberDO=new FooddrugChangeMemberDO();
				fooddrugChangeMemberDO.setName(fooddrugApplyMemberDO.getName());
				fooddrugChangeMemberDO.setSex(fooddrugApplyMemberDO.getSex());
				fooddrugChangeMemberDO.setNation(fooddrugApplyMemberDO.getNation());
				fooddrugChangeMemberDO.setChangeId(fooddrugChangeDO.getId());
				fooddrugChangeMemberDO.setRole(fooddrugApplyMemberDO.getRole());
				fooddrugChangeMemberDO.setJob(fooddrugApplyMemberDO.getJob());
				fooddrugChangeMemberDO.setPhone(fooddrugApplyMemberDO.getPhone());
				fooddrugChangeMemberDO.setCardType(fooddrugApplyMemberDO.getCardType());
				fooddrugChangeMemberDO.setCardNumber(fooddrugApplyMemberDO.getCardNumber());
				fooddrugChangeMemberDO.setEmail(fooddrugApplyMemberDO.getEmail());
				fooddrugChangeMemberDO.setTel(fooddrugApplyMemberDO.getTel());
				fooddrugChangeMemberDO.setCardFrontPicture(fooddrugApplyMemberDO.getCardFrontPicture());
				fooddrugChangeMemberDO.setCardBackPicture(fooddrugApplyMemberDO.getCardBackPicture());
				fooddrugChangeMemberDO.setHouseholdAddress(fooddrugApplyMemberDO.getHouseholdAddress());
				fooddrugChangeMemberDO.setHealthCertLeader(fooddrugApplyMemberDO.getHealthCertLeader());
				fooddrugChangeMemberDO.setHealthCertNumber(fooddrugApplyMemberDO.getHealthCertNumber());
				fooddrugChangeMemberDO.setHealthCertPicture(fooddrugApplyMemberDO.getHealthCertPicture());
				fooddrugChangeMemberDO.setCreateName(getUserName());
				fooddrugChangeMemberDO.setUpdateName(getUserName());
				fooddrugChangeMemberService.save(fooddrugChangeMemberDO);
			}
			FooddrugApplyLicenseQuery licenseQuery=new FooddrugApplyLicenseQuery();
			licenseQuery.setApplyId(applyId);
			List<FooddrugApplyLicenseDO> licenseList=fooddrugApplyLicenseService.queryList(licenseQuery);
			for (FooddrugApplyLicenseDO fooddrugApplyLicenseDO : licenseList) {
				FooddrugChangeLicenseDO fooddrugChangeLicenseDO=new FooddrugChangeLicenseDO();
				fooddrugChangeLicenseDO.setChangeId(fooddrugChangeDO.getId());
				fooddrugChangeLicenseDO.setPremises(fooddrugApplyLicenseDO.getPremises());
				fooddrugChangeLicenseDO.setBusinessArea(fooddrugApplyLicenseDO.getBusinessArea());
				fooddrugChangeLicenseDO.setBusinessNature(fooddrugApplyLicenseDO.getBusinessNature());
				fooddrugChangeLicenseDO.setBusinessCategory(fooddrugApplyLicenseDO.getBusinessCategory());
				fooddrugChangeLicenseDO.setBusinessProject(fooddrugApplyLicenseDO.getBusinessProject());
				fooddrugChangeLicenseDO.setSubjectFormats(fooddrugApplyLicenseDO.getSubjectFormats());
				fooddrugChangeLicenseDO.setBomemadeWine(fooddrugApplyLicenseDO.getBomemadeWine());
				fooddrugChangeLicenseDO.setCookedFood(fooddrugApplyLicenseDO.getCookedFood());
				fooddrugChangeLicenseDO.setDecoratingCake(fooddrugApplyLicenseDO.getDecoratingCake());
				fooddrugChangeLicenseDO.setFreshMilk(fooddrugApplyLicenseDO.getFreshMilk());
				fooddrugChangeLicenseDO.setNetworkSales(fooddrugApplyLicenseDO.getNetworkSales());
				fooddrugChangeLicenseDO.setSchoolCanteen(fooddrugApplyLicenseDO.getSchoolCanteen());
				fooddrugChangeLicenseDO.setStoreSales(fooddrugApplyLicenseDO.getStoreSales());
				fooddrugChangeLicenseDO.setCreateName(getUserName());
				fooddrugChangeLicenseDO.setUpdateName(getUserName());
				fooddrugChangeLicenseService.save(fooddrugChangeLicenseDO);
			}
			FooddrugApplyDeviceQuery deviceQuery=new FooddrugApplyDeviceQuery();
			deviceQuery.setApplyId(applyId);
			List<FooddrugApplyDeviceDO>  deviceList=fooddrugApplyDeviceService.queryList(deviceQuery);
			for (FooddrugApplyDeviceDO fooddrugApplyDeviceDO : deviceList) {
				FooddrugChangeDeviceDO fooddrugChangeDeviceDO=new FooddrugChangeDeviceDO();
				fooddrugChangeDeviceDO.setChangeId(fooddrugChangeDO.getId());
				fooddrugChangeDeviceDO.setDeviceName(fooddrugApplyDeviceDO.getDeviceName());
				fooddrugChangeDeviceDO.setNumber(fooddrugApplyDeviceDO.getNumber());
				fooddrugChangeDeviceDO.setSite(fooddrugApplyDeviceDO.getSite());
				fooddrugChangeDeviceDO.setCreateName(getUserName());
				fooddrugChangeDeviceDO.setUpdateName(getUserName());
				fooddrugChangeDeviceService.save(fooddrugChangeDeviceDO);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return fooddrugChangeDO;
	}





	@PostMapping("/fooddrug/api")
	@ResponseBody
	public Result queryFooddrugApi() {
		try {
			FooddrugApplyQuery query=new FooddrugApplyQuery();
			query.setUserId(getUserId());
			query.setApplyStatus(10);//展示申请通过的数据
			Integer fooddrugCount=fooddrugApplyService.queryCount(query);
			List<FooddrugApplyDO>	fooddrugList=fooddrugApplyService.queryList(query);
			return R.success(fooddrugList, fooddrugCount);
		} catch (ServiceException e) {
			return R.error();
		}
	}

	
	@PostMapping("/checkChange")
	@ResponseBody
	public Result checkChange(Integer applyId) {
		try {
	       FooddrugChangeQuery query=new FooddrugChangeQuery();
	       query.setUserId(getUserId());
	       query.setApplyId(applyId);
	       query.setStatus(1);
		   List<FooddrugChangeDO>  list= fooddrugChangeService.queryList(query);
		   Integer flag=0;
		   if(list!=null) {
		    for (FooddrugChangeDO fooddrugChangeDO : list) {
				 if(fooddrugChangeDO.getApplyStatus()<10) {
				  flag=1;//提交中有在审核中的或者未提交的
				 }
			 }
		   }			   
			 return  R.success(flag);
		} catch (ServiceException e) {
			e.printStackTrace();
		   return R.error();
		}
	}
	
	@PostMapping("/{applyId}")
	@ResponseBody
	public Result base(@PathVariable("applyId") Integer applyId) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
		FooddrugChangeDO fooddrugChangeDO=applyToChange(applyId);
		FlowInfoDO flowInfoDO=new FlowInfoDO();
		flowInfoDO.setUserId(getUserId());
		flowInfoDO.setItemId(fooddrugChangeDO.getId());
		flowInfoDO.setItemName(fooddrugChangeDO.getCompanyName()+FooddrugConstant.CHANGE_TITLE);
		flowInfoDO.setItemStatus(0);
		flowInfoDO.setComplete(0);
		flowInfoDO.setItemType(FooddrugConstant.CHANGE_ITEM_TYPE);
		flowInfoDO.setCreateName(getUserName());
		flowInfoDO.setUpdateName(getUserName());
		flowInfoService.save(flowInfoDO);
		map.put("id", fooddrugChangeDO.getId());
		map.put("companyName", fooddrugChangeDO.getCompanyName());
		map.put("companyLicenseList", queryAttach(fooddrugChangeDO.getId(),1));
		map.put("rentalContractList", queryAttach(fooddrugChangeDO.getId(),2));
		map.put("layoutList", queryAttach(fooddrugChangeDO.getId(),3));
		map.put("operatingProceduresList", queryAttach(fooddrugChangeDO.getId(),4));
		map.put("orientationMapList", queryAttach(fooddrugChangeDO.getId(),5));
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return R.success(map);
	}
	



	@PostMapping("/base")
	@ResponseBody
	public Result change(Integer changeId) {
		Map<String, Object> map =new HashMap<String, Object>();
		try {
			FooddrugChangeQuery query=new FooddrugChangeQuery();
			query.setId(changeId);
			FooddrugChangeDO fooddrugChangeDO = fooddrugChangeService.query(query);
			map.put("id", fooddrugChangeDO.getId());
			map.put("companyName", fooddrugChangeDO.getCompanyName());
			map.put("companyLicenseList", queryAttach(fooddrugChangeDO.getId(),1));
			map.put("rentalContractList", queryAttach(fooddrugChangeDO.getId(),2));
			map.put("layoutList", queryAttach(fooddrugChangeDO.getId(),3));
			map.put("operatingProceduresList", queryAttach(fooddrugChangeDO.getId(),4));
			map.put("orientationMapList", queryAttach(fooddrugChangeDO.getId(),5));
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}      
		return R.success(map);
	}


	@PostMapping("/saveBase")
	@ResponseBody
	public Result saveBase(FooddrugChangeDO fooddrugChangeDO) {
		Map<String, Object> map=new HashMap<String,Object>();
		try {
			fooddrugChangeDO.setUpdateName(getUserName());
			fooddrugChangeService.update(fooddrugChangeDO);
			fooddrugChangeImagesService.deleteChange(fooddrugChangeDO.getId());
			saveAttach(fooddrugChangeDO.getCompanyLicenseList(),true,fooddrugChangeDO.getId(),1);
			saveAttach(fooddrugChangeDO.getRentalContractList(),true,fooddrugChangeDO.getId(),2);
			saveAttach(fooddrugChangeDO.getLayoutList(),true,fooddrugChangeDO.getId(),3);
			saveAttach(fooddrugChangeDO.getOperatingProceduresList(),true,fooddrugChangeDO.getId(),4);
			saveAttach(fooddrugChangeDO.getOrientationMapList(),true,fooddrugChangeDO.getId(),5);
			map.put("changeId", fooddrugChangeDO.getId());
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return R.success(map);
	}

	@PostMapping("/agent")
	@ResponseBody
	public Result applyAgent(Integer changeId) {
		Map<String, Object> map=new HashMap<String,Object>();
		try {
			FooddrugChangeMemberQuery query=new FooddrugChangeMemberQuery();
			query.setChangeId(changeId);
			query.setRole(1);
			List<FooddrugChangeMemberDO> list=fooddrugChangeMemberService.queryList(query);
			FooddrugChangeMemberDO member=new FooddrugChangeMemberDO();
			if(list.size()!=0) {
				member= list.get(0);
				queryMember(member,map);
			}else {
				map.put("changeId",changeId);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return R.success(map);
	}


	@PostMapping("/saveAgent")
	@ResponseBody
	public Result saveAgent(FooddrugChangeMemberDO agent) {
		Map<String, Object> map=new HashMap<String,Object>();
		try {
			agent.setRole(1);
			if(agent.getId()==null) {
				agent.setCreateName(getUserName());
				agent.setUpdateName(getUserName());
				fooddrugChangeMemberService.save(agent);	
			}else {
				agent.setUpdateName(getUserName());
				fooddrugChangeMemberService.update(agent);	
			}
			map.put("changeId", agent.getChangeId());
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return R.success(map);
	}

	@PostMapping("/legal")
	@ResponseBody
	public Result applyLegal(@ModelAttribute("changeId") Integer changeId) {
		Map<String, Object> map=new HashMap<String,Object>();
		try {
			FooddrugChangeMemberQuery query=new FooddrugChangeMemberQuery();
			query.setChangeId(changeId);
			query.setRole(2);
			List<FooddrugChangeMemberDO> list=fooddrugChangeMemberService.queryList(query);
			FooddrugChangeMemberDO member=new FooddrugChangeMemberDO();
			if(list.size()!=0) {
				member= list.get(0);
				queryMember(member,map);
			}else {
				map.put("changeId",changeId);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		map.put("nationMap",NationUtil.getNationMap());//民族常量
		return R.success(map);
	}


	@PostMapping("/saveLegal")
	@ResponseBody
	public Result saveLegal(FooddrugChangeMemberDO legal) {
		Map<String, Object> map=new HashMap<String,Object>();
		try {
			legal.setRole(2);
			if(legal.getId()==null) {
				legal.setCreateName(getUserName());
				legal.setUpdateName(getUserName());
				fooddrugChangeMemberService.save(legal);
			}else {
				legal.setUpdateName(getUserName());
				fooddrugChangeMemberService.update(legal);
			}
			map.put("changeId",legal.getChangeId());
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return R.success(map);
	}
	
	@PostMapping("/license")
	@ResponseBody
	public Result changeLicense(@ModelAttribute("changeId") Integer changeId) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			FooddrugChangeLicenseQuery query =new FooddrugChangeLicenseQuery();
			query.setChangeId(changeId);
			List<FooddrugChangeLicenseDO> list=fooddrugChangeLicenseService.queryList(query);
			FooddrugChangeLicenseDO	 license=new FooddrugChangeLicenseDO();
			if(list.size()!=0) {
				license=list.get(0);
			}
			map.put("id",license.getId());
			map.put("premises",license.getPremises());
			map.put("businessArea",license.getBusinessArea());
			map.put("businessNature",license.getBusinessNature());
			map.put("subjectFormats",license.getSubjectFormats());
			map.put("businessCategory",license.getBusinessCategory());
			map.put("networkSales",license.getNetworkSales());
			map.put("storeSales",license.getStoreSales());
			map.put("schoolCanteen",license.getSchoolCanteen());
			map.put("businessProject",license.getBusinessProject());
			map.put("cookedFood",license.getCookedFood());
			map.put("bomemadeWine",license.getBomemadeWine());
			map.put("decoratingCake",license.getDecoratingCake());
			map.put("freshMilk",license.getFreshMilk());
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		map.put("changeId", changeId);
		return R.success(map);
	}
	
	@PostMapping("/saveLicense")
	@ResponseBody
	public Result saveLicense(FooddrugChangeLicenseDO license) {
		Map<String, Object> map =new HashMap<String,Object>();
 		try {
			if(license.getId()==null) {
				license.setCreateName(getUserName());
				license.setUpdateName(getUserName());
				fooddrugChangeLicenseService.save(license);
			}else {
				license.setUpdateName(getUserName());
				fooddrugChangeLicenseService.update(license);
			}
			map.put("changeId",license.getChangeId());
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return R.success(map);
	}



	@PostMapping("/employee")
	@ResponseBody
	public Result fooddrugEmployee(Integer changeId) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("nationMap",NationUtil.getNationMap());//民族常量
		FooddrugChangeMemberDO employee=new FooddrugChangeMemberDO();
		employee.setChangeId(changeId);
		queryMember(employee,map);
		return R.success();
	}

	/**
	 * 删除
	 */
	@PostMapping("/deleteEmployee/action")
	@ResponseBody
	public Result delete(@RequestParam Integer id) {
		try {
			fooddrugChangeMemberService.delete(id);
		} catch (Exception e) {
			return R.error();
		}
		return R.success();
	}


	@PostMapping("/saveEmployee")
	@ResponseBody
	public Result saveEmployee(FooddrugChangeMemberDO employee) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			employee.setRole(3);
			if(employee.getId()==null) {
				employee.setCreateName(getUserName());
				employee.setUpdateName(getUserName());
				fooddrugChangeMemberService.save(employee);	
			}else {
				employee.setUpdateName(getUserName());
				fooddrugChangeMemberService.update(employee);	
			}
			map.put("changeId",employee.getChangeId());
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return  R.success();
	}


	@PostMapping("/technician")
	@ResponseBody
	public Result applyTechnician(@ModelAttribute("changeId") Integer changeId) {
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("nationMap",NationUtil.getNationMap());//民族常量
		FooddrugChangeMemberDO technician=new FooddrugChangeMemberDO();
		technician.setChangeId(changeId);
		queryMember(technician,map);
		return R.success(map);
	}



	@PostMapping("/saveTechnician")
	@ResponseBody
	public Result saveTenician(FooddrugChangeMemberDO technician) {
		Map<String, Object> map=new HashMap<String,Object>();
		try {
			technician.setRole(4);
			if(technician.getId()==null) {
				technician.setCreateName(getUserName());
				technician.setUpdateName(getUserName());
				technician.setJob(1);
				fooddrugChangeMemberService.save(technician);
			}else {
				technician.setUpdateName(getUserName());
				fooddrugChangeMemberService.update(technician);
			}
			map.put("changeId",technician.getChangeId());
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return R.success();
	}



	@PostMapping("/saveManager")
	@ResponseBody
	public Result saveManager(FooddrugChangeMemberDO manager) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			manager.setRole(4);
			if(manager.getId()==null) {
				manager.setCreateName(getUserName());
				manager.setUpdateName(getUserName());
				manager.setJob(2);
				fooddrugChangeMemberService.save(manager);
			}else {
				manager.setUpdateName(getUserName());
				fooddrugChangeMemberService.update(manager);
			}
			map.put("changeId",manager.getChangeId());
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return R.success();
	}


	@PostMapping("/query/member")
	@ResponseBody
	public Result queryManager(Integer id) {
		Map<String, Object> map=new HashMap<String,Object>();
		if(id!=null) {
			try {
				map.put("nationMap",NationUtil.getNationMap());//民族常量
				FooddrugChangeMemberQuery query=new FooddrugChangeMemberQuery();
				query.setId(id);
				FooddrugChangeMemberDO member=fooddrugChangeMemberService.query(query);
				queryMember(member,map);
			  
			} catch (ServiceException e) {
				e.printStackTrace();
				return R.error();
			}
		}
		return R.success(map);
	}

	@RequestMapping("/member/api")
	@ResponseBody
	public Result queryMemberApi(@ModelAttribute("changeId") String changeId,@ModelAttribute("role") String role,@ModelAttribute("job") String job) {
		try {
			//当前用户
			FooddrugChangeMemberQuery query=new FooddrugChangeMemberQuery();
			query.setChangeId(Integer.valueOf(changeId));
			if(StringUtils.isNoneEmpty(role)) {
				query.setRole(Integer.valueOf(role));
			}
			if(StringUtils.isNoneEmpty(job)) {
				query.setJob(Integer.valueOf(job));
			}
			Integer queryCount = fooddrugChangeMemberService.queryCount(query);
			List<FooddrugChangeMemberDO> queryList=new ArrayList<FooddrugChangeMemberDO>();
			if (queryCount > 0) {
				queryList= fooddrugChangeMemberService.queryList(query);
			}
			return R.success(queryList, queryCount);
		} catch (ServiceException e) {
			return R.error();
		}
	}

	@PostMapping("/query/device")
	@ResponseBody
	public Result queryDevice(Integer id) {
		Map<String, Object> map=new HashMap<String,Object>();
		if(id!=null) {
			try {
				FooddrugChangeDeviceQuery query=new FooddrugChangeDeviceQuery();
				query.setId(id);
				FooddrugChangeDeviceDO device=fooddrugChangeDeviceService.query(query);
				map.put("changeId", device.getChangeId());
				map.put("deviceName", device.getDeviceName());
				map.put("number", device.getNumber());
				map.put("site", device.getSite());
			} catch (ServiceException e) {
				e.printStackTrace();
				return R.error();
			}
		}
		return R.success(map);
	}


	@PostMapping("/saveDevice")
	@ResponseBody
	public Result saveDevice(FooddrugChangeDeviceDO device) {
		Map<String, Object> map=new HashMap<String,Object>();
		try {
			if(device.getId()==null) {
				device.setCreateName(getUserName());
				device.setUpdateName(getUserName());
				fooddrugChangeDeviceService.save(device);
			}else {
				device.setUpdateName(getUserName());
				fooddrugChangeDeviceService.update(device);
			}
			map.put("changeId",device.getChangeId());
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
		return R.success();
	} 


	/**
	 * 删除
	 */
	@PostMapping("/deleteDevice/action")
	@ResponseBody
	public Result deleteDevice(@RequestParam Integer id) {
		try {
		fooddrugChangeDeviceService.delete(id);
		} catch (Exception e) {
		return R.error();
		}
		return R.success();
	}


	/**
	 * 查询项目列表接口
	 */
	@RequestMapping("/device/api")
	@ResponseBody
	public Result queryDeviceApi(@ModelAttribute("changeId") String changeId) {
		try {
			//当前用户
			FooddrugChangeDeviceQuery query=new FooddrugChangeDeviceQuery();
			query.setChangeId(Integer.valueOf(changeId));
			Integer queryCount = fooddrugChangeDeviceService.queryCount(query);
			List<FooddrugChangeDeviceDO> queryList=new ArrayList<FooddrugChangeDeviceDO>();
			if (queryCount > 0) {
				queryList = fooddrugChangeDeviceService.queryList(query);
			}
			return R.success(queryList, queryCount);
		} catch (ServiceException e) {
			return R.error();
		}
	}

	
	  /*
     * 食药申请提交
     * */
    @PostMapping("/saveApply")
    @ResponseBody
    public Result saveApply(FooddrugChangeDO fooddrugChangeDO) {
    	try {
			fooddrugChangeService.update(fooddrugChangeDO);
			FooddrugChangeQuery query=new FooddrugChangeQuery();
			query.setId(fooddrugChangeDO.getId());
			fooddrugChangeDO=fooddrugChangeService.query(query);
			FlowInfoDO flowInfoDO=new FlowInfoDO();
			flowInfoDO.setItemId(fooddrugChangeDO.getId());
			flowInfoDO.setItemType(FooddrugConstant.CHANGE_ITEM_TYPE);
			flowInfoDO.setItemStatus(1);
			flowInfoDO.setUpdateName(getUserName());
			flowInfoService.save(flowInfoDO);
			return R.success();
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
    }
    
    @PostMapping("/view")
    @ResponseBody
    public Result view(Integer id) {
    	Map<String, Object> map=new HashMap<String, Object>();
    	try {
    		FooddrugChangeQuery query=new FooddrugChangeQuery();
    		query.setId(id);
    		FooddrugChangeDO fooddrugChangeDO =fooddrugChangeService.query(query);
    		map.put("id", fooddrugChangeDO.getId());
    		map.put("companyName", fooddrugChangeDO.getCompanyName());
    		map.put("companyLicenseList", queryAttach(id,1));
    		map.put("rentalContractList", queryAttach(id,2));
    		map.put("layoutList", queryAttach(id,3));
    		map.put("operatingProceduresList", queryAttach(id,4));
    		map.put("orientationMapList", queryAttach(id,5));
    		FooddrugChangeMemberQuery memberQuery=new FooddrugChangeMemberQuery();
    		memberQuery.setChangeId(id);
    		memberQuery.setRole(1);
    		List<FooddrugChangeMemberDO> agentList= fooddrugChangeMemberService.queryList(memberQuery);
    		map.put("agentList",agentList);
    		memberQuery.setRole(2);
    		List<FooddrugChangeMemberDO> legalList= fooddrugChangeMemberService.queryList(memberQuery);
    		map.put("legalList",legalList);
    		memberQuery.setRole(3);
    		List<FooddrugChangeMemberDO> employeeList= fooddrugChangeMemberService.queryList(memberQuery);
    		map.put("employeeList",employeeList);
    		FooddrugChangeDeviceQuery deviceQuery=new FooddrugChangeDeviceQuery();
    		deviceQuery.setChangeId(id);
    		List<FooddrugChangeDeviceDO> deviceList= fooddrugChangeDeviceService.queryList(deviceQuery);
    		map.put("deviceList",deviceList);
    		FooddrugChangeLicenseQuery licenseQuery=new FooddrugChangeLicenseQuery();
    		licenseQuery.setChangeId(id);
    		List<FooddrugChangeLicenseDO> licenseList= fooddrugChangeLicenseService.queryList(licenseQuery);
    		FooddrugChangeLicenseDO license=new FooddrugChangeLicenseDO();
    		if(licenseList.size()!=0) {
    			license=licenseList.get(0);
    			map.put("businessProjectMap",BusinessProjectUtil.getProjectMap(license.getBusinessProject()));
    		}
    		map.put("license",license);
    		map.put("businessCategoryList",BusinessCategoryEnum.values());
    	} catch (ServiceException e) {
    		e.printStackTrace();
    		return R.error();
    	}
    	return R.success(map);
    }

    /**
     * 申请作废
     * */
    @PostMapping("/invalid/action")
    @ResponseBody
    public Result invalid(FlowInfoDO flowInfoDO) {
    	try {
    		flowInfoDO.setItemStatus(11);
    		flowInfoService.update(flowInfoDO);
    		FooddrugChangeDO changeDO=new FooddrugChangeDO();
    		changeDO.setId(flowInfoDO.getItemId());
    		changeDO.setApplyStatus(11);
    		fooddrugChangeService.update(changeDO);
    	} catch (ServiceException e) {
    		e.printStackTrace();
    		return R.error();
    	}
    	return R.success();
    }


	public Map<String, Object> queryMember(FooddrugChangeMemberDO member,Map<String, Object> map) {
		if(member.getId()!=null) {
			map.put("id", member.getId());
		}
		map.put("changeId", member.getChangeId());
		map.put("name", member.getName());
		map.put("sex", member.getSex());
		map.put("nation", member.getNation());
		map.put("role", member.getRole());
		map.put("job", member.getJob());
		map.put("cardType", member.getCardType());
		map.put("cardNumber", member.getCardNumber());
		map.put("cardFrontPicture", member.getCardFrontPicture());
		map.put("cardBackPicture", member.getCardBackPicture());
		map.put("phone", member.getPhone());
		map.put("tel", member.getTel());
		map.put("email", member.getEmail());
		map.put("householdAddress", member.getHouseholdAddress());
		map.put("healthCertLeader", member.getHealthCertLeader());
		map.put("healthCertNumber", member.getHealthCertNumber());
		map.put("healthCertPicture", member.getHealthCertPicture());
		return map;
	}


	/**
	 * 处理附件
	 *
	 * @param projectDO 项目
	 * @param isUpdate  是否更新项目（更新就先删除以前图片，再新增）
	 */
	private void saveAttach(List<String> attachList,Boolean isUpdate,Integer applyId,Integer type) throws ServiceException {
		if (CollectionUtils.isNotEmpty(attachList)) {
			if (isUpdate) {
				//删除附件(通过项目ID删除)
				fooddrugChangeImagesService.delete(applyId);
			}
			//添加附件
			for (String attach : attachList) {
				//判断url是否存在
				String url = StringUtils.isNotBlank(attach) ? attach : GlobalConstant.NOPIC;
				//写入文件
				FooddrugChangeImagesDO fooddrugChangeImagesDO = new FooddrugChangeImagesDO();
				fooddrugChangeImagesDO.setCreateName(getUserName());
				fooddrugChangeImagesDO.setUpdateName(getUserName());
				fooddrugChangeImagesDO.setChangeId(applyId);
				fooddrugChangeImagesDO.setStatus(1);
				fooddrugChangeImagesDO.setImg(url);
				fooddrugChangeImagesDO.setType(type);
				fooddrugChangeImagesService.save(fooddrugChangeImagesDO);
			}
		}
	}

	/**
	 * 查询附件
	 *
	 * @param projectDO 项目
	 * @param isUpdate  是否更新项目（更新就先删除以前图片，再新增）
	 */
	private List<String>  queryAttach(Integer applyId,Integer type) {
		List<String> list=new  ArrayList<String>();
		try {
			FooddrugChangeImagesQuery query=new FooddrugChangeImagesQuery();
			query.setChangeId(applyId);
			query.setType(type);
			List<FooddrugChangeImagesDO> imagesList=  fooddrugChangeImagesService.queryList(query);
			for (FooddrugChangeImagesDO fooddrugChangeImagesDO : imagesList) {
				list.add(fooddrugChangeImagesDO.getImg());
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return list;
	}


}
